local dianpei = fk.CreateSkill{
  name = "pegasus__dianpei",
  dynamic_desc = function(self, player)
    local des = table.map(player:getTableMark(self.name), function(d)
      return Fk:translate(d)
    end)
    return "pegasus__dianpei_inner:"..table.concat(des, "")
  end,
}

Fk:loadTranslationTable{
  ["pegasus__dianpei"] = "顛沛",
  [":pegasus__dianpei"] = "出牌阶段，你可以{①受到1点伤害，然后②分配一张牌，获得牌的角色③与一名角色拼点，赢的角色}改变上述项的顺序；你须将势力变更至其相同，否则本技能本回合失效，重置“背芒”。",

  [":pegasus__dianpei_inner"] = "出牌阶段，你可以{{1}}改变上述项的顺序；你须将势力变更至其相同，否则本技能本回合失效，重置“背芒”。",
  ["pegasus__dianpei_st"] = "①受到1点伤害，然后",
  ["pegasus__dianpei_nd"] = "②分配一张牌，获得牌的角色",
  ["pegasus__dianpei_rd"] = "③与一名角色拼点，赢的角色",

  ["#pegasus__dianpei-pindian"] = "顛沛：请与一名角色拼点",
  ["#pegasus__dianpei-order"] = "颠沛：请选择“颠沛”的顺序",
  ["#pegasus__dianpei-kingdom"] = "颠沛：将势力变更为 %arg ，否则本技能本回合失效，重置“背芒”",
}

local dianpei_des = {"pegasus__dianpei_st", "pegasus__dianpei_nd", "pegasus__dianpei_rd"}

---@param player ServerPlayer
---@param skill_name string
---@return ServerPlayer
local dianpei_st = function (player, skill_name)
  player.room:damage{
    damage = 1,
    from = player,
    to = player,
    skillName = skill_name,
  }
  return player
end

---@param player ServerPlayer
---@param skill_name string
---@return ServerPlayer|false
local dianpei_nd = function (player, skill_name)
  if player:isNude() then return false end
  local room = player.room
  local move = room:askToYiji(player, {
    cards = player:getCardIds("he"),
    targets = room:getOtherPlayers(player),
    skill_name = skill_name,
    max_num = 1,
    min_num = 1,
    cancelable = false,
  })
  for _, p in ipairs(room.alive_players) do
    if move[p.id] and #move[p.id] > 0 then
      return p
    end
  end
  return false
end

---@param player ServerPlayer
---@param skill_name string
---@return ServerPlayer|false
local dianpei_rd = function (player, skill_name)
  local room = player.room
  local targets = table.filter(room.alive_players, function (p) return player:canPindian(p) end)
  if #targets == 0 then return false end

  local target = room:askToChoosePlayers(player, {
    targets = targets,
    max_num = 1,
    min_num = 1,
    prompt = "#pegasus__dianpei-pindian",
    skill_name = skill_name,
    cancelable = false,
  })
  if #target == 0 then return false end

  local pindian = player:pindian(target, skill_name)
  local winner = pindian.results[target[1]].winner

  if winner then
    return winner
  end

  return false
end

local dianpei_func = {dianpei_st, dianpei_nd, dianpei_rd}

dianpei:addAcquireEffect(function (self, player)
  local room = player.room
  room:setPlayerMark(player, dianpei.name, dianpei_des)
end)

dianpei:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, dianpei.name, 0)
end)

dianpei:addEffect("active", {
  anim_type = "special",
  card_num = 0,
  target_num = 0,
  card_filter = Util.FalseFunc,
  target_filter = Util.FalseFunc,
  on_use = function (self, room, effect)
    local player = effect.from
    local mark = player:getTableMark(dianpei.name)
    local p = dianpei_func[table.indexOf(dianpei_des, mark[1])](player, dianpei.name)
    if p then
      p = dianpei_func[table.indexOf(dianpei_des, mark[2])](p, dianpei.name)
      if p then
        p = dianpei_func[table.indexOf(dianpei_des, mark[3])](p, dianpei.name)
        if p then
          local choice = room:askToChoice(p, {
            choices = {"①②③", "①③②", "②①③", "②③①", "③①②", "③②①"},
            skill_name = dianpei.name,
            prompt = "#pegasus__dianpei-order",
          })
          local new_mark = {}
          for i = 1, 3 do table.insert(new_mark, dianpei_des[table.indexOf({"①", "②", "③"}, choice[i])]) end
          room:setPlayerMark(player, dianpei.name, new_mark)
          if player.kingdom ~= p.kingdom and room:askToSkillInvoke(player, { skill_name = dianpei.name, prompt = "#pegasus__dianpei-kingdom:::"..Fk:translate(p.kingdom)} ) then
            room:setPlayerProperty(player, "kingdom", p.kingdom)
          else
            room:invalidateSkill(player, dianpei.name, "-turn", dianpei.name)
            player:setSkillUseHistory("pegasus__beimang", 0, Player.HistoryGame)
          end
        end
      end
    end
  end,
})

return dianpei